<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item :label="$t('发放类型')">
              <a-select v-model="queryParam.type">
                <a-select-option v-for="(item, index) in activityList" :key="index" :value="item.value">
                  {{ item.title }}
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :xl="5" :lg="6" :md="7" :sm="24">
            <a-form-item :label="$t('状态')">
              <a-select v-model="queryParam.state">
                <a-select-option v-for="(item, index) in stateList" :key="index" :value="item.value">
                  {{ item.title }}
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24" v-if="!tenantId">
            <a-form-item :label="$t('门店')">
              <a-select v-model="queryParam.tenantId">
                <a-select-option v-for="(item, i) in shops" :key="i" :value="item.value">
                  {{ item.title }}<span v-if="item.value != ''">（{{ item.value }}）</span></a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">{{$t('查询')}}</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">{{$t('重置')}}</a-button>
              <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? $t('收起') : $t('展开') }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'" />
              </a>
            </span>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <template v-if="toggleSearchStatus"> </template>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" type="primary" icon="plus" v-has="'BisCouponSetController:add'">{{$t('新增')}}</a-button>
    </div>

    <!-- table区域-begin -->
    <div>
      <a-table
        ref="table"
        size="middle"
        :scroll="{ x: true }"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        class="j-table-force-nowrap"
        @change="handleTableChange"
      >
        <span slot="action" slot-scope="text, record">
          <a @click="handleDetail(record, false)" v-has="'BisCouponSetController:edit'" v-if="(tenantId && tenantId == record.tenantId)||(!tenantId && record.tenantId == 0)">{{$t('编辑')}}</a>
          <a-popconfirm :title="$t('删除了该活动将立即失效，您确定删除该活动吗?')" @confirm="() => handleDelete(record.id)">
            <a style="margin-left: 8px" v-has="'BisCouponSetController:delete'" v-if="(tenantId && tenantId == record.tenantId)||(!tenantId && record.tenantId == 0)">{{$t('删除')}}</a>
          </a-popconfirm>
          <a style="margin-left: 8px" @click="handleDetail(record, true)">{{$t('详情')}}</a>
          <a style="margin-left: 8px" @click="handleCouponDetail(record)">{{$t('发券详情')}}</a>
        </span>
      </a-table>
    </div>

    <coupon-activity-modal ref="couponForm" @ok="loadData"></coupon-activity-modal>
    <send-coupon-details-modal ref="couponDetail"></send-coupon-details-modal>
  </a-card>
</template>

<script>
import '@/assets/less/TableExpand.less'
import Vue from 'vue'
import { mixinDevice } from '@/utils/mixin'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import { initDictOptions, filterMultiDictText } from '@/components/dict/JDictSelectUtil'
import CouponActivityModal from './modules/CouponActivityModal'
import SendCouponDetailsModal from './modules/SendCouponDetailsModal'

export default {
  name: 'SendCouponActivity',
  mixins: [JeecgListMixin, mixinDevice],
  components: {
    CouponActivityModal,SendCouponDetailsModal
  },
  data() {
    return {
      description: this.$t('优惠券活动'),
      queryParam: {
        column: 'createTime',
        order: 'desc',
        type: '',
        state: '1',
        name: '',
        tenantId: ''
      },
      // 表头
      columns: [
        {
          title: this.$t('发放类型'),
          align: 'center',
          dataIndex: 'type_dictText',
        },
        {
          title: this.$t('状态'),
          align: 'center',
          dataIndex: 'state_dictText',
        },
        {
          title: this.$t('发券明细'),
          align: 'center',
          dataIndex: 'bisCouponSetDetailList',
          customRender: function (t, r) {
            let text = []
            if (r.bisCouponSetDetailList.length > 0) {
              r.bisCouponSetDetailList.forEach((col) => {
                text.push(col.goodsId_dictText+'*'+col.amount)
              })
            }
            return text.join(',')
          },
        },
        {
          title: this.$t('有效期'),
          align: 'center',
          dataIndex: 'beginTime',
          customRender: (text, record) => {
            return record.beginTime + '~' + record.endTime
          },
        },
        {
          title: this.$t('活动说明'),
          align: 'center',
          dataIndex: 'comment',
        },
        {
          title: this.$t('操作列'),
          align: 'center',
          dataIndex: 'action',
          scopedSlots: { customRender: 'action' },
        },
      ],
      url: {
        list: '/coupon/bisCouponSet/list',
        delete: '/coupon/bisCouponSet/delete',
      },
      dictOptions: {},
      stateList: [],
      activityList: [],
      deviceList: [],
      tenantId: Vue.ls.get('TENANT_ID'),
      shops:[]
    }
  },
  async created() {
    if (!this.tenantId) {
      await initDictOptions('bis_shop,name,code').then((res) => {
        if (res.success) {
          this.shops = res.result
          this.shops.unshift({ value: '', title: this.$t('全部') })
        }
      })
      let dtl = {
        title: this.$t('门店编码'),
        align: 'center',
        dataIndex: 'tenantId',
        width: 100,
        customCell: (r, i) => {
          return {
            style: {
              whiteSpace: 'normal',
            },
          }
        },
      },
        dtl1 = {
          title: this.$t('门店名称'),
          align: 'center',
          dataIndex: 'tenantId_dictText',
          width: 140,
          customRender: (text, record) => {
            if (text) {
              return text
            }
            let x = this.shops.findIndex(s => { return s.value === record.tenantId.toString() })
            if (x > -1) {
              return this.shops[x].title
            }
            return record.tenantId
          },
          customCell: (r, i) => {
            return {
              style: {
                whiteSpace: 'normal',
              },
            }
          },
        }
      this.columns.unshift(dtl1)
      this.columns.unshift(dtl)
    }
  },
  computed: {
    importExcelUrl: function () {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    },
  },
  methods: {
    initDictConfig() {
      initDictOptions('bis_coupon_set.type').then((res) => {
        if (res.success) {
          this.activityList = res.result
          this.activityList.unshift({ title: this.$t('全部'), value: '' })
        }
      })
      initDictOptions('bis_coupon_set.state').then((res) => {
        if (res.success) {
          this.stateList = res.result
        }
      })
    },
    searchReset() {
      this.queryParam = {
        column: 'createTime',
        order: 'desc',
        state: '1',
        type: '',
        name: '',
        tenantId: ''
      }
      this.loadData(1)
    },
    modelClose() {
      this.loadData(1)
    },
    handleAdd(record) {
      this.$refs.couponForm.add(record)
      this.$refs.couponForm.title = this.$t('新增')
      this.$refs.couponForm.disableSubmit = false
    },
    handleDetail(record, state) {
      this.$refs.couponForm.edit(record, state)
      if (state) {
        this.$refs.couponForm.title = this.$t('查看')
      } else {
        this.$refs.couponForm.title = this.$t('编辑')
      }
      this.$refs.couponForm.disableSubmit = false
    },
    handleCouponDetail(record){
      this.$refs.couponDetail.show(record)
    }
  },
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>